package com.qf.sl.youshare.tasks;

import android.os.AsyncTask;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.utils.HttpTools;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/5.
 * 10:24
 */
public class UserRegisterTask extends AsyncTask<String, Integer, JSONObject> {

    private TaskProcessor processor;

    public UserRegisterTask(TaskProcessor processor) {
        this.processor = processor;
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONObject ret = null;

        if (params != null && params.length > 2) {
            String name = params[0];
            String pass = params[1];
            String confirmPass = params[2];

            String url = Constants.USER_URL + "?ac=register";

            HashMap<String, String> map = new HashMap<String, String>();

            map.put("user",name);
            map.put("password",pass);
            map.put("conform_password",confirmPass);

            byte[] bytes = HttpTools.post(url, map);

            if (bytes != null) {
                try {
                    String string = new String(bytes,"utf-8");
                    ret = new JSONObject(string);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

//            HttpPost request = new HttpPost("http://open.doushangshang.com/user/register.php");
//
//            HttpClient client = new DefaultHttpClient();
//
//            try {
//                HttpResponse response = client.execute(request);
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }

        return ret;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        if (processor != null) {
            processor.processResult(jsonObject);
        }
    }
}
